import { reqDetailData } from "@/api"

const state ={
  goodsDetailInfo:{}
}
const actions ={
  async getdetailInfo({commit},skuId){
    const result = await reqDetailData(skuId)
    if(result.code === 200){
      commit('GET_DETAIL_DATA',result.data)
    }else{
      console.log(result.message);
    }
  }
}
const mutations={
  GET_DETAIL_DATA(state,payload){
    state.goodsDetailInfo = payload
  },
  //判断商品选择是否高亮
  highlight(state,{totalId,rowId}){
    state.goodsDetailInfo.spuSaleAttrList.forEach(item => {
      if(item.id === totalId){
        item.spuSaleAttrValueList.forEach(i =>{
          i.isChecked = '0';
          if(i.id === rowId){
            i.isChecked = '1';
          }
        })
      }
    })
  },
  // 判断放大镜小图片的高亮点击
  UPDATE_SKUINFO_DEFAULT_IMGURL(state,skuImg){
    //循环遍历所有列表，使用排他
    state.goodsDetailInfo.skuInfo.skuImageList.forEach(item =>{
      item.isDefault = '0'
    })
    //当前点击的高亮
    skuImg.isDefault = '1'
    // 实现小图切换大图 也就是将被单击的小图的链接给到大图链接 
    state.goodsDetailInfo.skuInfo.skuDefaultImg = skuImg.imgUrl
  }
}
const getters={
  categoryView(state){
    return state.goodsDetailInfo.categoryView || {}
  },
  skuInfo(state){
    return state.goodsDetailInfo.skuInfo || {}
  },
  spuSaleAttrValue(state){
    return state.goodsDetailInfo.spuSaleAttrList || {}
  }
}
export default{
  namespaced:true,
  state,
  mutations,
  actions,
  getters
}